package cvslogic.elements;

import util.AuxiliaryFunctions;
import util.Constants;

import com.pavelvlasov.uml.Classifier;
import com.pavelvlasov.uml.ModelElement;

public class CompareClassifier {

	public boolean equalsRoot(Classifier newClassifier, Classifier oldClassifier) {
		return newClassifier.isRoot() == oldClassifier.isRoot();
	}

	public boolean equalsAbstract(Classifier newClassifier,
			Classifier oldClassifier) {
		return newClassifier.isAbstract() == oldClassifier.isAbstract();
	}

	public boolean equalsLeaf(Classifier newClassifier, Classifier oldClassifier) {
		return newClassifier.isLeaf() == oldClassifier.isLeaf();
	}

	public boolean equalsActive(Classifier newClassifier,
			Classifier oldClassifier) {
		return newClassifier.isActive() == oldClassifier.isActive();
	}

	public boolean equalsGeneralizations(Classifier newClassifier,
			Classifier oldClassifier) {

		Object[] newGeneralizations = newClassifier.getGeneralizations()
				.toArray();
		Object[] oldGeneralizations = oldClassifier.getGeneralizations()
				.toArray();

		for (Object generalization : newGeneralizations) {
			if (!oldClassifier.getGeneralizations().contains(generalization)) {
				return false;
			}
		}

		for (Object generalization : oldGeneralizations) {
			if (!newClassifier.getGeneralizations().contains(generalization)) {
				return false;
			}
		}

		/*
		 * if
		 * (!newClassifier.getExtends().toString().equals(newClassifier.getExtends
		 * ().toString())){ return false; }
		 */

		return true;

	}

	public boolean equalsImports(Classifier newClassifier,
			Classifier oldClassifier) {

		Classifier[] newGeneralizations = (Classifier[]) newClassifier
				.getImports().toArray();
		Classifier[] oldGeneralizations = (Classifier[]) oldClassifier
				.getImports().toArray();

		for (Classifier generalization : newGeneralizations) {
			if (!oldClassifier.getImports().contains(generalization)) {
				return false;
			}
		}

		for (Classifier generalization : oldGeneralizations) {
			if (!newClassifier.getImports().contains(generalization)) {
				return false;
			}
		}

		// getExtends : return the supertype;

		return true;

	}

	public String getAbstracts(Classifier newClassifier,
			Classifier oldClassifier) {
		return "Old: " + oldClassifier.isAbstract() + " - New: "
				+ newClassifier.isAbstract();
	}

	public String getRoots(Classifier newClassifier, Classifier oldClassifier) {
		return "Old: " + oldClassifier.isRoot() + " - New: "
				+ newClassifier.isRoot();
	}

	public String getLeafs(Classifier newClassifier, Classifier oldClassifier) {
		return "Old: " + oldClassifier.isLeaf() + " - New: "
				+ newClassifier.isLeaf();
	}

	public String getActives(Classifier newClassifier, Classifier oldClassifier) {
		return "Old: " + oldClassifier.isActive() + " - New: "
				+ newClassifier.isActive();
	}

	public String getGeneralizations(Classifier newClassifier,
			Classifier oldClassifier) {
		return "Old: "
				+ (AuxiliaryFunctions.concatenates(oldClassifier
						.getGeneralizations().toArray()))
				+ " - New: "
				+ (AuxiliaryFunctions.concatenates(newClassifier
						.getGeneralizations().toArray()));

	}

	public String getImports(Classifier newClassifier, Classifier oldClassifier) {
		return "Old: "
				+ (AuxiliaryFunctions.concatenates(oldClassifier.getImports()
						.toArray()))
				+ " - New: "
				+ (AuxiliaryFunctions.concatenates(newClassifier.getImports()
						.toArray()));

	}

	public void compareProperties(Classifier oldClass, Classifier newClass) {
		AuxiliaryFunctions.print(Constants.ABSTRACT, equalsAbstract(newClass,
				oldClass), getAbstracts(newClass, oldClass));
		AuxiliaryFunctions.print(Constants.ROOT,
				equalsRoot(newClass, oldClass), getRoots(newClass, oldClass));
		AuxiliaryFunctions.print(Constants.LEAF,
				equalsLeaf(newClass, oldClass), getLeafs(newClass, oldClass));
		AuxiliaryFunctions.print(Constants.ACTIVE, equalsActive(newClass,
				oldClass), getActives(newClass, oldClass));
		AuxiliaryFunctions.print(Constants.GENETALIZATIONS,
				equalsGeneralizations(newClass, oldClass), getGeneralizations(
						newClass, oldClass));
	}

}
